﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.IO.Compression;
using System;

namespace BF
{
    public static class GZipUtil
    {
        public static bool IsGZipData(byte[] bytes)
        {
            if (bytes != null && bytes.Length >= 8)
            {
                return bytes[0] == 31
                    && bytes[1] == 139
                    && bytes[2] == 8
                    && bytes[3] == 0
                    && bytes[4] == 0
                    && bytes[5] == 0
                    && bytes[6] == 0
                    && bytes[7] == 0;
            }
            return false;
        }

        public static byte[] Compress(byte[] bytes)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress))
                {
                    zipStream.Write(bytes, 0, bytes.Length);
                }
                return ms.ToArray();
            }
        }


        public static byte[] Decompress(byte[] bytes)
        {
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress))
                {
                    using (MemoryStream resultStream = new MemoryStream())
                    {
                        zipStream.CopyTo(resultStream);
                        return resultStream.ToArray();
                    }
                }
            }
        }
    }
}